package com.dl.shuiyin;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.huantansheng.easyphotos.EasyPhotos;
import com.huantansheng.easyphotos.models.album.entity.Photo;
import com.huantansheng.easyphotos.utils.bitmap.SaveBitmapCallBack;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.os.Environment;
import android.view.View;

import android.view.Menu;
import android.view.MenuItem;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private ImageView ivMainPic;
    private FrameLayout fl_out;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ivMainPic = (ImageView) findViewById(R.id.iv_mainPic);
        fl_out = (FrameLayout) findViewById(R.id.fl_out);


        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectClick(view);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            fl_out.setDrawingCacheEnabled(true);

            fl_out.setDrawingCacheEnabled(true);
            fl_out.buildDrawingCache();
            Bitmap restultImage = fl_out.getDrawingCache();

//            try {
//                FileUtils.saveBitmapToFile(fileName, restultImage);
//                commonCallbackEntity.callback(fileName);
//                return;
//
//            }
            String path=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + "/shuiyin";
            EasyPhotos.saveBitmapToDir(this, path, "haha", restultImage, true, new SaveBitmapCallBack() {
                @Override
                public void onSuccess(File file) {
                    Toast.makeText(MainActivity.this, "保存成功~", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onIOFailed(IOException exception) {

                }

                @Override
                public void onCreateDirFailed() {

                }
            });
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void selectClick(View view){
        EasyPhotos.createAlbum(this, true, GlideEngine.getInstance())
                .setFileProviderAuthority("com.huantansheng.easyphotos.demo.fileprovider")
                .setCount(1)
                .setOriginalMenu(true, true, null)
                .start(101);
    }

//    public void jiashuiyin(Uri bitmapUri){
//        if (bitmapUri==null) {
//            Toast.makeText(this, "没选图片", Toast.LENGTH_SHORT).show();
//        }
//
//        //这一步如果图大的话会耗时，但耗时不长，你可以在异步操作。另外copy出来的bitmap在确定不用的时候记得回收，如果你用Glide操作过copy出来的bitmap那就不要回收了，否则Glide会报错。
//        Bitmap watermark = BitmapFactory.decodeResource(getResources(), R.drawable.watermark).copy(Bitmap.Config.RGB_565, true);
//        try {
//            bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(bitmapUri)).copy(Bitmap.Config.ARGB_8888, true);
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        }
//        //给图片添加水印的api
//        EasyPhotos.addWatermark(watermark, bitmap, 1080, 20, 20, true);
//
//        bitmapView.setVisibility(View.VISIBLE);
//        bitmapView.setImageBitmap(bitmap);
//        Toast.makeText(SampleActivity.this, "水印在左下角", Toast.LENGTH_SHORT).show();
//
//        break;
//    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
//        if (albumItemsAdView != null) {
//            if (albumItemsAdView.getParent() != null) {
//                ((FrameLayout) (albumItemsAdView.getParent())).removeAllViews();
//            }
//        }
//        if (photosAdView != null) {
//            if (photosAdView.getParent() != null) {
//                ((FrameLayout) (photosAdView.getParent())).removeAllViews();
//            }
//        }
        if (RESULT_OK == resultCode) {
            //相机或相册回调
            if (requestCode == 101) {
                //返回对象集合：如果你需要了解图片的宽、高、大小、用户是否选中原图选项等信息，可以用这个
                ArrayList<Photo> resultPhotos = data.getParcelableArrayListExtra(EasyPhotos.RESULT_PHOTOS);

                //返回图片地址集合时如果你需要知道用户选择图片时是否选择了原图选项，用如下方法获取
                boolean selectedOriginal = data.getBooleanExtra(EasyPhotos.RESULT_SELECTED_ORIGINAL, false);

                try {
                   Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(resultPhotos.get(0).uri)).copy(Bitmap.Config.ARGB_8888, true);
                    ivMainPic.setImageBitmap(bitmap);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }

//                selectedPhotoList.clear();
//                selectedPhotoList.addAll(resultPhotos);
//                adapter.notifyDataSetChanged();
//                rvImage.smoothScrollToPosition(0);

                return;
            }

//            //为拼图选择照片的回调
//            if (requestCode == 102) {
//
//                ArrayList<Photo> resultPhotos =
//                        data.getParcelableArrayListExtra(EasyPhotos.RESULT_PHOTOS);
//                if (resultPhotos.size() == 1) {
//                    resultPhotos.add(resultPhotos.get(0));
//                }
//                selectedPhotoList.clear();
//                selectedPhotoList.addAll(resultPhotos);
//
//                EasyPhotos.startPuzzleWithPhotos(this, selectedPhotoList, Environment.getExternalStorageDirectory().getAbsolutePath(), "AlbumBuilder", 103, false, GlideEngine.getInstance());
//                return;
//            }
//
//            //拼图回调
//            if (requestCode == 103) {
//                Photo puzzlePhoto = data.getParcelableExtra(EasyPhotos.RESULT_PHOTOS);
//                selectedPhotoList.clear();
//                selectedPhotoList.add(puzzlePhoto);
//                adapter.notifyDataSetChanged();
//                rvImage.smoothScrollToPosition(0);
//            }


        } else if (RESULT_CANCELED == resultCode) {
            Toast.makeText(this, "cancel", Toast.LENGTH_SHORT).show();
        }
    }
}